<?php
/**
 * [JishiGou] (C)2005 - 2099 Cenwor Inc.
 *
 * This is NOT a freeware, use is subject to license terms
 *
 * @Filename article.mod.php $
 *
 * @Author http://www.jishigou.net $
 *
 * @Date 2013-05-21 17:04:54 1032001564 850043636 23519 $
 */
if (! defined ( 'IN_JISHIGOU' )) {
	exit ( 'invalid request' );
}
class ModuleObject extends MasterObject {
	var $Member;
	function ModuleObject($config) {
		$this->MasterObject ( $config );
		
		$this->TopicLogic = jlogic ( 'topic' );
		if (MEMBER_ROLE_TYPE != 'admin') {
			if (! $this->Config ['article_open']) {
				$this->Messager ( "管理员已关闭文章功能", "index.php?mod=topic" );
			}
		}
		$this->Member = $this->_member ();
		
		$this->Execute ();
	}
	function Execute() {
		ob_start ();
		switch ($this->Code) {
			case 'pevent' :
				$this->pevent ();
				break;
			case 'editarticle' :
				$this->editarticle ();
				break;
			case 'myarticle' :
				$this->myarticle ();
				break;
			case 'followarticle' :
				$this->followarticle ();
				break;
			case 'detail' :
				$this->articleDetail ();
				break;
			case 'del' :
				$this->delarticle ();
				break;
			case 'alluser' :
				$this->getAllUser ();
				break;
			case 'export_to_excel' :
				$this->ExportToExcel ();
				break;
			case 'upDown':
				$this->upDownArticle();
				break;
			default :
				$this->main ();
				break;
		}
		$body = ob_get_clean ();
		
		$this->ShowBody ( $body );
	}
	function main() {
		$FormHandler = jlib ( 'form' );
		
		load::logic ( 'article' );
		$articleLogic = new articleLogic ();
		
		$hot_article = $articleLogic->getHotarticle ();
		
		$uids = array ();
		if ($uids = $articleLogic->getDaRen ()) {
			$hd_daren = $this->TopicLogic->GetMember ( $uids );
		}
		
		$article_type = array ();
		$query = $this->DatabaseHandler->Query ( 'select * from ' . TABLE_PREFIX . 'article_sort order by id' );
		while ( $rsdb = $query->GetRow () ) {
			$acount = DB::result_first ( 'select count(*) from ' . TABLE_PREFIX . "article where type_id = '$rsdb[id]' and is_delete='0'" );
			$article_type [$rsdb ['id']] ['count'] = $acount;
			$article_type [$rsdb ['id']] ['type'] = $rsdb ['type'];
		}
		
		$where = " 1 ";
		$type = intval ( $this->Get ['type'] );
		if ($type) {
			$where .= " and a.type_id = '$type' ";
			$tclass [$type] = " class='fred'";
			$hid_type = $type;
		} else {
			$tclass [0] = " class='fred'";
		}
		
		if ($this->Config ['same_city']) {
			$choose_province = intval ( $this->Get ['province'] );
			$choose_city = intval ( $this->Get ['city'] );
			$choose_area = intval ( $this->Get ['area'] );
			$nocity = $this->Get ['nocity'];
			
			if ($choose_province) {
				$hid_province = $choose_province;
			}
			if ($this->Member ['province']) {
				$province_id = DB::result_first ( 'select id from ' . TABLE_PREFIX . "common_district where name = '" . $this->Member ['province'] . "' and `upid` = '0'" );
			} else {
				$province_id = DB::result_first ( 'select id from ' . TABLE_PREFIX . "common_district where `upid` = '0' order by list" );
			}
			$query = $this->DatabaseHandler->Query ( 'select * from ' . TABLE_PREFIX . "common_district where `upid` = '0' order by list" );
			while ( $rsdb = $query->GetRow () ) {
				$province [$rsdb ['id']] ['value'] = $rsdb ['id'];
				$province [$rsdb ['id']] ['name'] = $rsdb ['name'];
			}
			$province_id = $choose_province ? $choose_province : $province_id;
			$province_list = $FormHandler->Select ( "province", $province, $province_id, " style=\"display:none\" onchange=\"changeProvince();\"" );
			
			if ($province_id) {
				$hid_province = $province_id;
				if ($this->Member ['city']) {
					$city_id = DB::result_first ( "select id from `" . TABLE_PREFIX . "common_district` where `name` = '" . $this->Member ['city'] . "'" );
				} else {
					$city_id = DB::result_first ( "select id from `" . TABLE_PREFIX . "common_district` where `upid` = '$province_id' order by `list`" );
				}
				$query = $this->DatabaseHandler->Query ( "select * from `" . TABLE_PREFIX . "common_district` where `upid` = '$province_id' order by `list`" );
				while ( $rsdb = $query->GetRow () ) {
					$city [$rsdb ['id']] ['value'] = $rsdb ['id'];
					$city [$rsdb ['id']] ['name'] = $rsdb ['name'];
				}
				$city_id = $choose_city ? $choose_city : $city_id;
				$city_name = DB::result_first ( "select `name` from `" . TABLE_PREFIX . "common_district` where `id` = '$city_id' " );
				$city_list = $FormHandler->Select ( "city", $city, $city_id, " style=\"display:none\" onchange=\"changeCity();\"" );
				
				if ($city_id) {
					$query = $this->DatabaseHandler->Query ( "select * from `" . TABLE_PREFIX . "common_district` where `upid` = '$city_id' order by `list`" );
					while ( $rsdb = $query->GetRow () ) {
						$area [$rsdb ['id']] = $rsdb ['name'];
					}
				}
			}
			
			if ($nocity) {
				$cclass ['no'] = " class='fred'";
				$hid_city = $city_id;
				$hid_no_city = $nocity;
			} else {
				if ($city_id && ! $choose_area) {
					$where .= " and a.city_id = '$city_id' ";
					$cclass [$city_id] = " class='fred'";
					$hid_city = $city_id;
				} else {
					if ($choose_area) {
						$where .= " and a.area_id = '$choose_area' ";
						$cclass [$choose_area] = " class='fred'";
						$hid_city = $city_id;
						$hid_area = $choose_area;
					}
				}
			}
		}
		
		$where .= " and a.verify = 1 and a.is_delete='0' ";
		$param = array (
				'where' => $where,
				'order' => 'order by a.posttime DESC , a.app_num DESC ',
				'limit' => '',
				'page' => true,
				'perpage' => 10,
				'page_url' => "index.php?mod=article&province=$choose_province&city=$choose_city&area=$choose_area&type=$type&nocity=$nocity" 
		);
		$return = array ();
		$return = $articleLogic->getarticleInfo ( $param );
		$count = $return ['count'] ? $return ['count'] : 0;
		$article = $return ['article_list'];
		$page_arr = $return ['page'];
		$app = $return ['app'];
		
		$member = $this->Member;
		if ($member ['medal_id']) {
			$medal_list = $this->TopicLogic->GetMedal ( $member ['medal_id'], $member ['uid'] );
		}
		$this->Title = "文章";
		include (template ( 'article' ));
	}
	function getAllUser() {
		load::logic ( 'article' );
		$articleLogic = new articleLogic ();
		
		$id = ( int ) $this->Get ['id'];
		$type = $this->Get ['type'];
		$title = DB::result_first ( "select `title` from `" . TABLE_PREFIX . "article` where `id` = '$id'" );
		
		$param = array (
				'limit' => '',
				'page' => true,
				'perpage' => 20,
				'page_url' => "index.php?mod=article&code=alluser&id=$id&type=$type" 
		);
		if ($type == 'app') {
			$param ['where'] = " a.id = '$id' and a.app = 1 and a.play = 0 ";
			$param ['order'] = " order by a.app_time ";
			$name = "报名";
		} else {
			$param ['where'] = " a.id = '$id' and a.play = 1 ";
			$param ['order'] = " order by a.play_time ";
			$name = "参与";
		}
		
		$return = array ();
		if ($return = $articleLogic->getAllUser ( $param, $type )) {
			extract ( $return );
		}
		
		$my_member = $this->Member;
		if ($my_member ['medal_id']) {
			$medal_list = $this->TopicLogic->GetMedal ( $my_member ['medal_id'], $my_member ['uid'] );
		}
		
		$this->Title = $title . "的所有" . $name . "者";
		include (template ( 'article_member' ));
	}
	function delarticle() {
		$id = ( int ) $this->Get ['id'];
		if ($id < 1) {
			$this->Messager ( "请选择要删除的文章", - 1 );
		}
		
		load::logic ( 'article' );
		$articleLogic = new articleLogic ();
		$return = $articleLogic->delarticle ( $id );
		if ($return == 1) {
			$this->Messager ( "该文章已有报名者，您不能删除该文章，如要删除，请联系管理员", - 1 );
		} elseif ($return == 2) {
			$this->Messager ( "文章不存在或已删除", - 1 );
		} else {
			$this->Messager ( "删除文章成功", "index.php?mod=article&code=myarticle" );
		}
	}
	function ExportToExcel() {
		$id = intval ( $this->Get ['id'] );
		$list = array ();
		
		load::logic ( 'article' );
		$articleLogic = new articleLogic ();
		$param = array (
				'where' => " a.id = '$id' and a.is_delete='0' " 
		);
		$return = $articleLogic->getarticleInfo ( $param );
		$article = $return ['article_list'] [$id];
		
		$list [0] [0] = '标题:';
		$list [0] [1] = $article ['title'];
		$list [1] [0] = '时间:';
		$list [1] [1] = $article ['fromt'] . "-" . $article ['tot'];
		$list [2] [0] = '地点:';
		$list [2] [1] = $article ['province'] . $article ['city'] . $article ['area'] . $article ['address'];
		$list [3] [0] = '发起人:';
		$list [3] [1] = $article ['nickname'];
		$list [4] [0] = '参与人数:';
		$list [4] [1] = $article ['play_num'] . "人";
		
		$list [5] = array ();
		
		$list [6] [0] = '申请者';
		$j = 1;
		foreach ( unserialize ( $article ['need_app_info'] ) as $value ) {
			$list [6] [$j] = $value ['name'];
			$j ++;
		}
		$list [6] [$j] = '留言';
		
		$i = 7;
		$param = array (
				'where' => " id = '$id' and play = 1 ",
				'order' => " order by a.play_time " 
		);
		$member = $articleLogic->getAllUser ( $param );
		$member_list = ( array ) $member ['member'];
		foreach ( $member_list as $member ) {
			$app_info = ( array ) unserialize ( $member ['app_info'] );
			$list [$i] [0] = $member ['nickname'];
			$k = 1;
			foreach ( $app_info as $value ) {
				$list [$i] [$k] = $value;
				$k ++;
			}
			$i ++;
		}
		Load::lib ( 'php-excel' );
		$xls = new Excel_XML ( $this->Config ['charset'] );
		$xls->addArray ( $list );
		$xls->generateXML ( 'article-' . date ( "YmdHis" ) );
		exit ();
	}
	function articleDetail() {
		$id = intval ( $this->Get ['id'] );
		
		load::logic ( 'article' );
		$articleLogic = new articleLogic ();
		$param = array (
				'where' => " a.id = '$id' " 
		);
		
		$return = $articleLogic->getarticleInfo ( $param );
		$rs = $return ['article_list'] [$id];
		
		if (! $rs||$rs['is_delete']=='1') {
			$this->Messager ( "文章不存在或已删除", - 1 );
		}
		
		if (! $rs ['verify'] || $rs ['verify'] == 0) {
			if ($rs ['postman'] != MEMBER_ID) {
				$this->Messager ( "文章还在审核中", - 1 );
			}
		}
		//为文章点击率加1
		$articleLogic->addClickArticle($id);
		$from = array ();
		if ($rs ['item'] == 'qun' && $rs ['item_id'] > 0) {
			$rs ['qunname'] = DB::Result_first ( " select `name` from `" . TABLE_PREFIX . "qun` where `qid` = '{$rs['item_id']}' " );
			$from ['name'] = $this->Config ['changeword'] ['weiqun'] . '--' . $rs ['qunname'];
			$from ['url'] = get_full_url ( '', 'index.php?mod=qun&qid=' . $rs ['item_id'] );
		} else {
			// f NEDU
			if (defined ( 'NEDU_MOYO' )) {
				if ($rs ['item'] && $rs ['item_id']) {
					$app = nlogic ( 'com.object' )->get_info ( $rs ['item'], $rs ['item_id'] );
					if ($app) {
						$from = array (
								'name' => $app ['object_name'],
								'url' => $app ['object_url'] 
						);
					}
				}
			}
			// ndif
		}
		
		$app_member_arr = $articleLogic->getAllUser ( array (
				'where' => " a.id = '$id' and a.app = 1 and a.play = 0 ",
				'order' => " order by a.app_time ",
				'limit' => " limit 6 " 
		), 'app' );
		$app_count = $app_member_arr ['count'];
		$app_member = $app_member_arr ['member'];
		
		$play_member_arr = $articleLogic->getAllUser ( array (
				'where' => " a.id = '$id' and a.play = 1  ",
				'order' => " order by a.play_time ",
				'limit' => " limit 6 " 
		), 'play' );
		$play_count = $play_member_arr ['count'];
		$play_member = $play_member_arr ['member'];
		
		$member = $this->Member;
		if ($member ['medal_id']) {
			$medal_list = $this->TopicLogic->GetMedal ( $member ['medal_id'], $member ['uid'] );
		}
		
		Load::functions ( 'app' );
		$gets = array (
				'mod' => 'article',
				'code' => "detail",
				'id' => $id 
		);
		$page_url = 'index.php?' . url_implode ( $gets );
		
		$options = array (
				'page' => true,
				'perpage' => 5,
				'page_url' => $page_url 
		);
		$topic_info = app_get_topic_list ( 'article', $id, $options );
		$topic_list = array ();
		if (! empty ( $topic_info )) {
			$topic_list = $topic_info ['list'];
			$page_arr ['html'] = $topic_info ['page'] ['html'];
			$no_from = true;
		}
		$this->item = 'article';
		$this->item_id = $id;
		
		$set_qun_closed = 1;
		$set_article_closed = 1;
		$set_fenlei_closed = 1;
		$this->Title = $rs ['title'];
		include (template ( 'article_dateil' ));
	}
	function myarticle() {
		load::logic ( 'article' );
		$articleLogic = new articleLogic ();
		
		$hot_article = $articleLogic->getHotarticle ();
		
		$uids = array ();
		if ($uids = $articleLogic->getDaRen ()) {
			$hd_daren = $this->TopicLogic->GetMember ( $uids );
		}
		
		$type = $this->Get ['type'];
		$uid = intval ( $this->Get ['uid'] );
		if ($uid && $uid != MEMBER_ID) {
			$user = DB::result_first ( "select `nickname` from `" . TABLE_PREFIX . "members` where `uid` = '$uid'" );
		} else {
			$user = "我";
		}
		$uid = ($uid && $uid != MEMBER_ID) ? $uid : MEMBER_ID;
		
		$param = array (
				'perpage' => "10",
				'page' => true 
		);
		$return = array ();
		
		if ($type == 'store') {
			$this->Title = $user . "收藏的文章";
			$param ['where'] = "";
			$param ['order'] = " order by a.lasttime desc,a.app_num desc,a.posttime desc ";
			$param ['page_url'] = "index.php?mod=article&code=myarticle&type=store&uid=$uid";
			$return = $articleLogic->getarticles ( $param );
		} else if ($type == 'new') {
			jtable ( 'members' )->clean_new_remind ( 'article_new', $uid );
			
			$this->Title = "最近更新的文章";
			$param ['uid'] = $uid;
			$param ['page_url'] = "index.php?mod=article&code=myarticle&type=new";
			$return = $articleLogic->getNewarticle ( $param );
		} else {
			$this->Title = $user . "的文章";
			$param ['where'] = " a.postman = '$uid' and a.verify = 1 ";
			$param ['order'] = " order by a.lasttime desc,a.app_num desc,a.posttime desc ";
			$param ['page_url'] = "index.php?mod=article&code=myarticle&uid=$uid";
			$return = $articleLogic->getarticleInfo ( $param );
		}
		
		$rs = $return ['article_list'];
		$count = $return ['count'];
		$page_arr = $return ['page'];
		
		$member = $this->Member;
		if ($member ['medal_id']) {
			$medal_list = $this->TopicLogic->GetMedal ( $member ['medal_id'], $member ['uid'] );
		}
		
		include (template ( 'my_article' ));
	}
	function followarticle() {
		load::logic ( 'article' );
		$articleLogic = new articleLogic ();
		
		$hot_article = $articleLogic->getHotarticle ();
		
		$uids = array ();
		if ($uids = $articleLogic->getDaRen ()) {
			$hd_daren = $this->TopicLogic->GetMember ( $uids );
		}
		
		$buddyids = get_buddyids ( MEMBER_ID );
		if ($buddyids) {
			$uid_list = implode ( ',', $buddyids );
		}
		
		$type = $this->Get ['type'];
		
		$param = array (
				'perpage' => "10",
				'page' => true 
		);
		$return = array ();
		if ($uid_list) {
			if ($type == 'part') {
				$param ['where'] = " m.play = 1 and m.fid in ($uid_list) ";
				$param ['order'] = " order by a.lasttime desc,a.app_num desc,a.posttime desc ";
				$param ['page_url'] = "index.php?mod=article&code=followarticle&type=part";
				$this->Title = "他们参与的文章";
				$return = $articleLogic->getarticles ( $param );
			} else {
				$param ['where'] = " a.postman in ($uid_list) and a.verify = 1 and a.is_delete='0' ";
				$param ['order'] = " order by a.lasttime desc,a.app_num desc,a.posttime desc ";
				$param ['page_url'] = "index.php?mod=article&code=followarticle";
				$page_url = "index.php?mod=article&code=followarticle";
				$this->Title = "他们管理的文章";
				$return = $articleLogic->getarticleinfo ( $param );
			}
			if ($return) {
				extract ( $return );
				$rs = $article_list;
			}
		} else {
			$this->Messager ( "你没有关注的人哦、可以通过找人来关注你感兴趣的人", "index.php?mod=profile&code=search" );
		}
		
		$member = $this->Member;
		if ($member ['medal_id']) {
			$medal_list = $this->TopicLogic->GetMedal ( $member ['medal_id'], $member ['uid'] );
		}
		
		include (template ( 'follow_article' ));
	}
	function pevent() {
		if (MEMBER_ID < 1) {
			$this->Messager ( "你需要先登录才能继续本操作", 'index.php?mod=login' );
		}
		
		if (MEMBER_ROLE_TYPE != 'admin') {
			load::logic ( 'article' );
			$articleLogic = new articleLogic ();
			$is_allowed = $articleLogic->allowedCreate ( MEMBER_ID, $this->Member );
		}
		if ($is_allowed) {
			$this->Messager ( $is_allowed );
		}
		
		$free = 'checked';
		$all = 'checked';
		$info = array ();
		$info = ConfigHandler::get ( 'profilearticleinfo' );
		
		$FormHandler = jlib ( 'form' );
		$query = $this->DatabaseHandler->Query ( "select * from `" . TABLE_PREFIX . "article_sort` order by `id` " );
		while ( $rsdb = $query->GetRow () ) {
			$rs [$rsdb ['id']] ['value'] = $rsdb ['id'];
			$rs [$rsdb ['id']] ['name'] = $rsdb ['type'];
		}
		$article_type = $FormHandler->Select ( "type", $rs, $val ['type_id'] );
		
		$member = $this->Member;
		if ($member ['medal_id']) {
			$medal_list = $this->TopicLogic->GetMedal ( $member ['medal_id'], $member ['uid'] );
		}
		
		$this->Title = "发起文章";
		include (template ( 'article_create' ));
	}
	function editarticle() {
		$id = ( int ) $this->Get ['id'];
		$postman = DB::result_first ( "select postman from " . TABLE_PREFIX . "article where id = '$id'" );
		if ($postman != MEMBER_ID) {
			$this->Messager ( "你无权修改该文章" );
		}
		$free = 'checked';
		$all = 'checked';
		
		$info = array ();
		$info = ConfigHandler::get ( 'profilearticleinfo' );
		
		$val = array ();
		if ($id) {
			$act = "edit";
			$query = $this->DatabaseHandler->Query ( "select * from `" . TABLE_PREFIX . "article` where `id` = '$id'" );
			$val = $query->GetRow ();
			$item_id = $val ['item_id'];
			$title = $val ['title'];
			
			$content = $val ['content'];
			if ($val ['image']) {
				$image = $val ['image'];
			}
			
			$qualification = unserialize ( $val ['qualification'] );
			if (count ( $qualification )) {
				$qua = "checked";
				$all = "";
				if ($qualification ['fans_num']) {
					$fans = "checked";
					$fans_num = $qualification ['fans_num'];
				}
				if ($qualification ['same_city']) {
					$same_city = "checked";
				}
				if ($qualification ['inqun']) {
					$inqun = " checked ";
				}
			}
			$need_app_info = unserialize ( $val ['need_app_info'] );
			if (count ( $need_app_info )) {
				foreach ( $need_app_info as $value ) {
					$info [$value] ['checked'] = "checked";
				}
			}
		}
		
		$FormHandler = jlib ( 'form' );
		$query = $this->DatabaseHandler->Query ( "select * from " . TABLE_PREFIX . "article_sort order by `id`" );
		while ( $rsdb = $query->GetRow () ) {
			$rs [$rsdb [id]] ['value'] = $rsdb ['id'];
			$rs [$rsdb [id]] ['name'] = $rsdb ['type'];
		}
		$article_type = $FormHandler->Select ( "type", $rs, $val ['type_id'] );
		
		$query = $this->DatabaseHandler->Query ( "select * from `" . TABLE_PREFIX . "common_district` where `upid` = 0 order by `list`" );
		while ( $rsdb = $query->GetRow () ) {
			$province [$rsdb ['id']] ['value'] = $rsdb ['id'];
			$province [$rsdb ['id']] ['name'] = $rsdb ['name'];
		}
		
		$member = $this->Member;
		
		$this->Title = "修改文章";
		include (template ( 'article_create' ));
	}
	function upDownArticle() {
		$id = ( int ) $this->Get ['id'];
		if ($id < 1) {
			$this->Messager ( "请选择要删除的文章", - 1 );
		}
		load::logic ( 'article' );
		$articleLogic = new articleLogic ();
		$return = $articleLogic->setUpDownArticle($id,$this->Get['status']);
		if ($return == 1) {
			$this->Messager ( "你Y坑我吧?这什么操作?", - 1 );
		} elseif ($return == 2) {
			$this->Messager ( "你已经对该文章做出了评价了!", - 1 );
		} else {
			$this->Messager ( "评价成功", "index.php?mod=article&code=detai&id={$id}" );
		}
	}
	function _member() {
		$member = jsg_member_info ( MEMBER_ID );
		
		return $member;
	}
}